
package cn.edu.tsinghua.hpc.gsmtest;

import cn.edu.tsinghua.hpc.gsmtest.utils.RILUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.ToggleButton;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class CallTester extends Activity {

    static private boolean DEBUG = true;

    static private String TAG = "CallTester";

    static final int MENU_PREFERENCES = 0;

    private List<Map<String, Object>> mData;

    private ListView mListView = null;

    private ToggleButton mRunButton;

    private static final String INDEX = "index";

    private static final String VALUE = "value";

    // for statistics
    private int dialCount = 0;

    private int dialOKCount = 0;

    private void incMessageSentCount() {

        dialCount++;
        dialOKCount++;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mListView = new ListView(this);
        mData = new ArrayList<Map<String, Object>>();
        init();
    }

    private void init() {

        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.call_button_item, null);
        mListView.addFooterView(layout);
        mRunButton = (ToggleButton) mListView.findViewById(R.id.call_test_button);
        mRunButton.setOnCheckedChangeListener(mRunListener);
    }

    private Timer mTimer = new Timer();

    private class CallTimerTask extends TimerTask {

        @Override
        public void run() {
            incMessageSentCount();
            if (dialCount <= Preferences.getIntById(R.string.pref_call_times)) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        String address = Preferences.getStringById(R.string.pref_target_number);
                        int time = Preferences.getIntById(R.string.pref_call_length);
                        RILUtils.dialPhone(address, time);
                        update();
                    }
                });
            } else {
                this.cancel();
                mTimer.cancel();

            }

        }

    }

    private CompoundButton.OnCheckedChangeListener mRunListener = new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                mTimer = new Timer();
                dialCount = 0;
                dialOKCount = 0;
                int period = Preferences.getIntById(R.string.pref_call_internal) * 1000;
                mTimer.scheduleAtFixedRate(new CallTimerTask(), 0, period);
            } else {
                mTimer.cancel();
            }

        }

    };

    private void update() {
        mData.clear();
        mListView.clearTextFilter();
        if (DEBUG)
            Log.d(TAG, this.getString(R.string.call_length));

        addColumn(this.getString(R.string.call_length), Preferences
                .getStringById(R.string.pref_call_length));
        addColumn(this.getString(R.string.call_internal), Preferences
                .getStringById(R.string.pref_call_internal));
        addColumn(this.getString(R.string.current_info), getInfo());
        addColumn(this.getString(R.string.phone_state), getState());
        addColumn(this.getString(R.string.statistic), getStat());
        addColumn(this.getString(R.string.target_number), Preferences
                .getStringById(R.string.pref_target_number));

        SimpleAdapter adapter = new SimpleAdapter(this, mData, R.layout.call_tester_item,
                new String[] {
                        INDEX, VALUE
                }, new int[] {
                        R.id.tag, R.id.value
                });
        mListView.setAdapter(adapter);
        setContentView(mListView);
    }

    private String getState() {
        // TODO Auto-generated method stub
        return "Waiting....";
    }

    private String getInfo() {
        return "";
    }

    private String getStat() {
        // TODO
        return "" + dialCount + "/" + dialOKCount + "/"
                + Preferences.getStringById(R.string.pref_call_times);
    }

    private void addColumn(String tag, String value) {
        Map<String, Object> item;
        item = new HashMap<String, Object>();
        item.put(INDEX, tag);
        item.put(VALUE, value);
        mData.add(item);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        menu.clear();
        menu.add(0, MENU_PREFERENCES, 0, R.string.menu_preferences).setIcon(
                android.R.drawable.ic_menu_preferences);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case MENU_PREFERENCES:
                Intent intent = new Intent(this, Preferences.class);
                startActivity(intent);
                break;
            default:
                return false;
        }
        return true;
    }

    @Override
    public void onResume() {
        super.onResume();
        update();
    }

}
